//
//  MSHomeViewSource.m
//  MSJD
//
//  Created by xiangbo on 2018/9/29.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSHomeViewSource.h"
#import "MSSearchViewController.h"

@interface MSHomeViewSource ()<MSBasePageViewDelegate,MSHomeHeadViewDelegate>

@end

@implementation MSHomeViewSource

#pragma mark - Property
- (MSHomeHeadView *)headView{
    if (!_headView) {
        _headView = [[MSHomeHeadView alloc] initWithFrame:CGRectZero headType:MSHeadViewTypeFood];
        _headView.customDelegate = self;
    }
    return _headView;
}

- (MSHomePageView *)homeView{
    if (!_homeView) {
        _homeView = [[MSHomePageView alloc] init];
        for (NSString *key in MSIdentifierDic.allKeys) {
            [_homeView registetDetailView:MSIdentifierDic[key] identifier:key];
        }
        _WEAKSELF
        _homeView.tableview.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
            [weakSelf refreshTableData];
        }];
        _homeView.delegate = self;
    }
    return _homeView;
}

#pragma mark - MSBasePageViewDelegate
- (NSString*)identifier:(id)data indexPath:(NSIndexPath *)indexPath{
    MSFoodData *moduleData = data;
    NSString *identify = moduleData.showType;
    return identify;
}

- (void)refreshTableData{
    _WEAKSELF
    [MSRequestClient GET:@"v1/index" parameters:@{@"kid" :@"1"} responseClassType:MSFoodResponse.class success:^(id responseObject) {
        [weakSelf.homeView.tableview.mj_header endRefreshing];
        MSFoodResponse *foodResponse = responseObject;
        if (foodResponse.data.count > 0) {
            weakSelf.homeView.dataArray = (id)foodResponse.data;
            [weakSelf.homeView.tableview reloadData];
        }
    } failure:^(NSError *error) {
        [weakSelf.homeView.tableview.mj_header endRefreshing];
        NSLog(@"-------");
    }];
}

- (void)refreshHomeLocalTitle {
    if (self.headView) {
        [self.headView refreshLocalTitle];
    }
}

#pragma mark - MSHomeHeadViewDelegate
/** 跳转到搜索界面 */
- (void)showSearchViewController {
    MSSearchViewController *searchVC = [MSSearchViewController loadSearchViewControllerWithKind:1 title:@""];
    MSNavigationController *nav = [[MSNavigationController alloc] initWithRootViewController:searchVC];
    [[UIViewController currentViewController] presentViewController:nav animated:NO completion:nil];
}
@end
